// pages/updateLogistics/updateLogistics.js
import api from '../../api/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    upreceiptlist: {
      shippingOrderNumber: ""
    },
    updatelog: {
      companyId: "",   //公司id
      shippingOrderNumber: "", //出通单号
      currentLocation: "已签收", //当前位置信息
      latitude: "", //纬度
      longitude: "", //经度
    },
    name: '',
    number: '',
    shippingOrderNumber: '',
    fileList: [], //文件上传时 在前端展示上传的图片
  },
  updatelogList(){
    let that = this
    api.post('/app/logistics/updateLogisticsInfo', that.data.updatelog)
    .then(res => {
    })
  },
  // beforeRead(file){
  //   console.log(file);
  // },
  deleteImg(event) {
    const delIndex = event.detail.index
    const {
      fileList
    } = this.data
    fileList.splice(delIndex, 1)
    this.setData({
      fileList
    })
  },
  beforeRead(file) {
    var format = file.detail.file.tempFilePath.split('.').pop();
    if (format != "png" && format != "jpg" && format != "jpeg") {
      return wx.showToast({
        title: '只能上传.png、.jpg、.jpep 格式',
        icon: 'none',
        duration: 2000,
      })
    } else {
      file.detail.callback(true)
    }
  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    let that = this
    let url = wx.getStorageSync('headurl')
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: url + '/app/receiptOrder/addReceiptOrder',
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        // 上传完成需要更新 fileList
        const {
          fileList = []
        } = that.data;
        fileList.push({
          ...file,
          url: JSON.parse(res.data).data
        });
        that.setData({
          fileList
        });
        wx.showToast({
          icon: 'none',
          title: "上传成功",
          duration: 2000
        })
      },
    });
  },
  onChangeName(e) {
    let that = this
    that.setData({
      name: e.detail,
    })
  },
  onChangeNumber(e) {
    let that = this
    that.setData({
      number: e.detail,
    })
  },
  onClickLeft() {
    wx.navigateBack()
  },
  uploadfile() {
    let that = this
    if (!(/^[\u4E00-\u9FA5A-Za-z]+$/.test(that.data.name))) {
      wx.showToast({
        title: '姓名有误',
        duration: 1000,
        icon: 'none'
      });
    } else {
      if (!/^1[3456789]\d{9}$/.test(that.data.number)) {
        // 手机号格式不正确，提示用户
        wx.showToast({
          title: '请输入正确的手机号',
          duration: 1000,
          icon: 'none'
        });
        return false
      } else {
        if (that.data.fileList.length==0) {
          wx.showToast({
            icon: 'none',
            title: "请上传回执单",
            duration: 2000
          });
        } else {
          let addReceiptOrderReq = {
            signatory: that.data.name,
            signatoryPhone: that.data.number,
            shippingOrderNumber: that.data.upreceiptlist.shippingOrderNumber,
            receiptFileAddress: that.data.fileList[0].url
          }
          api.post('/app/receiptOrder/confirmReceiptOrder', addReceiptOrderReq)
            .then(res => {
              if (res.code == 200) {
                setTimeout(function() {
                  wx.showToast({
                    icon:'none',
                    title: "上传成功",
                    duration: 2000
                 });
               }, 2000);
               that.updatelogList()
                wx.navigateBack()
              } else {
                wx.showToast({
                  icon: 'none',
                  title: "上传失败",
                  duration: 2000
                });
              }
            })
            .catch(err => {
            })
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
     let companyid=wx.getStorageSync('companyid')
    that.setData({
      'upreceiptlist.shippingOrderNumber': options.shippingOrderNumber,
      'updatelog.shippingOrderNumber': options.shippingOrderNumber,
      'updatelog.companyId':companyid
    })
  },
})